package redis.client.nedis;

import redis.client.nedis.reply.IReply;

/**
 * 基于netty的redis客户端
 *
 * @author gaodong
 */
public class Nedis {

    final Client client;

    public Nedis(String ip, int port, String auth, boolean cluster) {
        client = new Client(ip, port, auth);
    }

    private void close() {
        client.close();
    }

    public static Builder builder() {
        return new Builder();
    }

    static class Builder {
        private String ip;
        private int port;
        private String auth;
        private boolean cluster;

        public Builder setIp(String ip) {
            this.ip = ip;
            return this;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public Builder setAuth(String auth) {
            this.auth = auth;
            return this;
        }

        public Builder setCluster(boolean cluster) {
            this.cluster = cluster;
            return this;
        }

        public Nedis build() {
            return new Nedis(ip, port, auth, cluster);
        }
    }

    public IReply<?>[] testpipe() {
        return client.testpipe(rq -> {
            //System.out.println(rq.getReply().data());
        });
    }

    public IReply<?> testset() {
        return client.set("k1", "v1", rq -> {
            System.out.println(rq.getReply().data());
        });
    }

    public static void main(String[] args) throws InterruptedException {

        /*Nedis nedis = Nedis
                .builder()
                .setIp("10.228.130.177")
                .setPort(7003)
                .setAuth("configMS7001")
                .setCluster(true)
                .build();*/

        Nedis nedis = Nedis
                .builder()
                .setIp("127.0.0.1")
                .setPort(6379)
                .build();

        for (int i = 0; i < 50; i++) {
            long s = System.nanoTime();
            nedis.testset();
            long e = System.nanoTime();
            System.out.println("all=" + (e - s) / 1e6 + "ms");
            System.out.println();
        }

        //nedis.close();
    }
}